#include <stdio.h>
#include <string.h>
#include <windows.h>

void searchDirectory(const char *basePath, const char *keyword) {
    char path[MAX_PATH];
    WIN32_FIND_DATAA fd;

    snprintf(path, MAX_PATH, "%s\\*", basePath);

    HANDLE hFind = FindFirstFileA(path, &fd);
    if (hFind == INVALID_HANDLE_VALUE) return;

    do {
        if (strcmp(fd.cFileName, ".") == 0 || strcmp(fd.cFileName, "..") == 0)
            continue;

        char fullPath[MAX_PATH];
        snprintf(fullPath, MAX_PATH, "%s\\%s", basePath, fd.cFileName);

        if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            // 递归搜索子目录
            searchDirectory(fullPath, keyword);
        } else {
            // 文件名包含关键词则打印路径
            if (strstr(fd.cFileName, keyword) != NULL) {
                printf("Found: %s\n", fullPath);
            }
        }

    } while (FindNextFileA(hFind, &fd));

    FindClose(hFind);
}

int main() {
    SetConsoleOutputCP(CP_UTF8);
    SetConsoleCP(CP_UTF8);
    char startPath[MAX_PATH];
    char keyword[256];

    printf("请输入起始目录（例如 C:\\\\Users\\\\Admin）：");
    fgets(startPath, sizeof(startPath), stdin);
    startPath[strcspn(startPath, "\n")] = 0;  // 去掉换行符

    printf("请输入查找关键词（文件名部分即可）：");
    fgets(keyword, sizeof(keyword), stdin);
    keyword[strcspn(keyword, "\n")] = 0;

    printf("\n开始搜索...\n");
    searchDirectory(startPath, keyword);
    printf("搜索完成。\n");

    return 0;
}
